package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.db.soul.*;
import com.itheima.domain.vo.*;
import com.itheima.service.db.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SoulManager {

    @Reference
    SoulExamService soulExamService;

    @Reference
    SoulQuestionService soulQuestionService;

    @Reference
    SoulQuestionOptionService soulQuestionOptionService;

    @Reference
    SoulUserLockService soulUserLockService;

    @Reference
    SoulReportService soulReportService;

    @Reference
    SoulResultService soulResultService;

    @Reference
    UserInfoService userInfoService;



    public ResponseEntity findSoul() {
        List<SoulExamVo> soulExamVoList = new ArrayList<>();

        Long userId = UserHolder.get().getId();
        List<SoulExam> soulExamList = soulExamService.findAll();
        if (CollUtil.isNotEmpty(soulExamList)) {
            for (SoulExam soulExam : soulExamList) {
                Long examId = soulExam.getId();
                List<SoulQuestion> soulQuestionList = soulQuestionService.findByLevel(examId);

                List<QuestionsVo> questions = new ArrayList<>();
                if (CollUtil.isNotEmpty(soulQuestionList)) {
                    for (SoulQuestion soulQuestion : soulQuestionList) {
                        String questionId = soulQuestion.getId();
                        List<SoulQuestionOption> soulQuestionOptions = soulQuestionOptionService.findByQuestionId(questionId);

                        List<OptionsVo> options = new ArrayList<>();
                        if (CollUtil.isNotEmpty(soulQuestionOptions)) {
                            for (SoulQuestionOption soulQuestionOption : soulQuestionOptions) {

                                OptionsVo optionsVo = new OptionsVo();
                                optionsVo.setId(soulQuestionOption.getId());
                                optionsVo.setOption(soulQuestionOption.getOptions());
                                options.add(optionsVo);
                            }
                        }

                        QuestionsVo questionsVo = new QuestionsVo();
                        questionsVo.setId(soulQuestion.getId());
                        questionsVo.setOptions(options);
                        questionsVo.setQuestion(soulQuestion.getQuestion());
                        questions.add(questionsVo);

                    }
                }

                SoulExamVo soulExamVo = new SoulExamVo();
                soulExamVo.setCover(soulExam.getCover());
                soulExamVo.setId(soulExam.getId().toString());
                soulExamVo.setLevel(soulExam.getLevel().toString());
                soulExamVo.setName(soulExam.getName());
                soulExamVo.setStar(soulExam.getStar());
                soulExamVo.setQuestions(questions);

                SoulUserLock soulUserLock = soulUserLockService.findByUserId(userId, soulExam.getId());
                if (soulUserLock != null) {
                    soulExamVo.setIsLock(soulUserLock.getIsLock());

                }else{
                    soulExamVo.setIsLock(1);
                }

                SoulReport soulReport = soulReportService.findOne(userId,soulExam.getId());

                if (soulReport != null){

                    SoulResult soulResult = soulResultService.findByScore(soulReport.getScore());
                    soulExamVo.setReportId(soulResult.getId().toString());
                }
                soulExamVoList.add(soulExamVo);
            }
        }
        soulExamVoList.get(0).setIsLock(0);

        return ResponseEntity.ok(soulExamVoList);
    }

    public ResponseEntity submitSoul(List<Map<String, String>> list) {
        int score = 0;
        Long userId = UserHolder.get().getId();
        Long soulExamId = null;

        if (CollUtil.isNotEmpty(list)) {
            for (Map<String, String> map : list) {
                score += soulQuestionOptionService.findScore(map.get("questionId"), map.get("optionId"));
                soulExamId = soulQuestionService.findExamIdByQuestionId(map.get("questionId"));
            }
        }
        if (soulExamId < 3){
            soulUserLockService.saveOrUpdate(userId,soulExamId+1);
        }



        soulReportService.saveOrUpdateScore(userId,score,soulExamId);


        SoulResult soulResult = soulResultService.findByScore(score);


        return ResponseEntity.ok(soulResult.getId().toString());
    }

    public ResponseEntity findResult(String id) {
        SoulResult soulResult = soulResultService.findById(NumberUtil.parseLong(id));
        SoulResultVo soulResultVo = new SoulResultVo();

        soulResultVo.setConclusion(soulResult.getContent());
        soulResultVo.setCover(soulResult.getCover());
        List<Dimension> dimensions = new ArrayList<>();
        List<SimilarYou> similarYou = new ArrayList<>();

        Dimension dimension = null;
        dimension = new Dimension();
        dimension.setKey("外向");
        dimension.setValue(soulResult.getExtroversion().toString());
        dimensions.add(dimension);

        dimension = new Dimension();
        dimension.setKey("判断");
        dimension.setValue(soulResult.getJudgement().toString());
        dimensions.add(dimension);

        dimension = new Dimension();
        dimension.setKey("抽象");
        dimension.setValue(soulResult.getAbstraction().toString());
        dimensions.add(dimension);

        dimension = new Dimension();
        dimension.setKey("理性");
        dimension.setValue(soulResult.getRetionality().toString());
        dimensions.add(dimension);

        PageBeanVo page = userInfoService.findByPage(1, 6);

        List<UserInfo> items = (List<UserInfo>) page.getItems();
        if (CollUtil.isNotEmpty(items)) {
            for (UserInfo userInfo : items) {
                SimilarYou sly = new SimilarYou();
                sly.setId(userInfo.getId().intValue());
                sly.setAvatar(userInfo.getAvatar());
                similarYou.add(sly);
            }

        }

        soulResultVo.setDimensions(dimensions);
        soulResultVo.setSimilarYou(similarYou);
        return ResponseEntity.ok(soulResultVo);
    }
}
